package com.jyb.qiyefuwu.view.activity.usercenter;


import android.app.Dialog;
import android.content.Intent;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.jyb.qiyefuwu.R;
import com.jyb.qiyefuwu.databinding.ActivitySalesOrderDetailsBinding;
import com.jyb.qiyefuwu.entity.OderDetailsEntity;
import com.jyb.qiyefuwu.entity.ServiceNodeBean;
import com.jyb.qiyefuwu.eventmodel.MakOverOrderDeleteItemEvent;
import com.jyb.qiyefuwu.eventmodel.VoucherCommitEvent;
import com.jyb.qiyefuwu.uitls.DialogFunction;
import com.jyb.qiyefuwu.uitls.DialogHelper;
import com.jyb.qiyefuwu.uitls.DialogTextReturnValue;
import com.jyb.qiyefuwu.uitls.PointUtils;
import com.jyb.qiyefuwu.uitls.TextViewBindingAdapter;
import com.jyb.qiyefuwu.view.dialog.OderNodeDialog;
import com.jyb.qiyefuwu.viewmodel.SaleListViewModel;
import com.jyb.qiyefuwu.viewmodel.SalesOderDetailsModel;
import com.sgb.link.constant.RequestCode;
import com.sgb.link.router.PathCapital;
import com.sgb.link.router.PathEnterpriseServices;
import com.sgb.link.router.PathUser;
import com.swgk.core.base.aroute.AppRouteConstant;
import com.swgk.core.base.model.entity.ContactsEntitiy;
import com.swgk.core.base.model.entity.InvoicingInfoEntity;
import com.swgk.core.common.base.BaseActivity;
import com.swgk.core.common.utils.ActivityJump;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.DensityUtils;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;


/**
 * 作者:陈方凯
 * 日期:2021/09/27
 * 说明:销售订单详情
 */
@Route(path = PathEnterpriseServices.SalesOrderDetailsActivity)
public class SalesOrderDetailsActivity extends BaseActivity<SalesOderDetailsModel, ActivitySalesOrderDetailsBinding> implements ItemInputTextView.OnWholeItemClickListener {
    public static final int TRADE_MARK = 110119;//商标
    public static final int PATENT = 110118;//专利
    public static final int COPYRIGHT = 110117;//版权
    public static final int MARK = 1157;//备注
    public static final int CONTACTS = 1158;//联系人
    public static final int INVOICE = 1159;//发票
    /**
     * 0删除   1关闭   2取消   3接单
     */
    private int operationOrder;
    /**
     * TRADE_MARK商标   PATENT专利   COPYRIGHT版权
     */
    private Integer zhiChanType;
    private String oderId;
    private String type;
    private OderNodeDialog oderNodeDialog;
    private SaleListViewModel saleListViewModel;
    private List<ServiceNodeBean> serviceNodeBean;
    private OderDetailsEntity mData;

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().removeStickyEvent(this);
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_sales_order_details;
    }

    @Override
    protected void createViewModel() {
        mViewModel = new ViewModelProvider(this).get(SalesOderDetailsModel.class);
        saleListViewModel = new ViewModelProvider(this).get(SaleListViewModel.class);
        EventBus.getDefault().register(this);
    }


    @Override
    protected void init() {
        saleListViewModel.buyerOrSeller = 0;
        zhiChanType = getIntent().getIntExtra("zhiChanType", 110118);
        oderId = getIntent().getStringExtra("oderId");
        observer();
        initData();
        initListen();
    }

    private void initListen() {
        mBinding.tvOrderReceiving.setOnClickListener(this);//确认接单
        mBinding.tvCloseOder.setOnClickListener(this);//关闭订单
        mBinding.tvDeleteOder.setOnClickListener(this);//删除订单
        mBinding.llFoldPersonInfo.setOnClickListener(this);//个人信息展开
        mBinding.llFoldOderInfo.setOnClickListener(this);//订单信息展开
        mBinding.llFoldInvoiceInfo.setOnClickListener(this);//开票信息展开
        mBinding.tvCheckPerson.setOnClickListener(this);//切换联系人
        mBinding.tvCheckInvoice.setOnClickListener(this);//选择发票
        mBinding.llMark.setOnClickListener(this);//修改备注
        mBinding.lookProgress.setOnClickListener(this);
        mBinding.backView.setOnClickListener(this);
        mBinding.rvGoTo.setOnClickListener(this);
        mBinding.itvDatabase.setOnWholeItemClickListener(this);
        mBinding.itvJournal.setOnWholeItemClickListener(this);
        mBinding.imageTalkIm.setOnClickListener(this);
        mBinding.itvVoucher.setOnWholeItemClickListener(this);
        mBinding.tvCommitVoucher.setOnClickListener(this);
    }

    private void initData() {
        mBinding.tvMark.setEnable(false);
        mBinding.tvIsSeller.setText("买方");//买卖方
        mBinding.tvTitle.setText("销售订单详情");//买卖方
        mBinding.itvDatabase.setArrowRightImage(R.mipmap.icn_right_blue);
        mBinding.itvVoucher.setArrowRightImage(R.mipmap.icn_right_blue);
        getDetails();
    }

    private void observer() {
        mViewModel.oderData.observe(this, new Observer<OderDetailsEntity>() {
            @Override
            public void onChanged(OderDetailsEntity data) {
                mData = data;
                mViewModel.process(mViewModel.oderData.getValue().getId());
                setNetData(data);
            }
        });
        mViewModel.dataText.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                mBinding.tvEndTime.setText(s);
            }
        });
        mViewModel.checkData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                MToast.showToast(SalesOrderDetailsActivity.this, "修改成功");
                getDetails();
            }
        });

        mViewModel.nodeList.observe(this, new Observer<List<ServiceNodeBean>>() {
            @Override
            public void onChanged(List<ServiceNodeBean> serviceNodeBeans) {
                serviceNodeBean = serviceNodeBeans;
                oderNodeDialog = null;
                if ("3".equals(mData.getOrderStatus())) {
                    mBinding.tvEndTime.setText(serviceNodeBean.get(serviceNodeBean.size() - 1).getTips());
                    setDrawable(false);
                }
            }
        });
        saleListViewModel.delete_item.observe(this, new Observer<Long>() {
            @Override
            public void onChanged(Long aLong) {
                EventBus.getDefault().post(new MakOverOrderDeleteItemEvent(aLong));
                switch (operationOrder) {
                    case 0:
                        MToast.showToast(SalesOrderDetailsActivity.this, "删除成功");
                        finish();
                        break;
                    case 1:
//                        MToast.showToast(SalesOrderDetailsActivity.this, "订单已取消");
//                        mViewModel.removeTimer();
                        mBinding.tvEndTime.setText("卖方主动取消订单");
                        getDetails();//刷新详情
                        break;
                    case 2:
//                        MToast.showToast(SalesOrderDetailsActivity.this, "订单已关闭");
//                        mViewModel.removeTimer();
                        mBinding.tvEndTime.setText("卖方主动关闭订单");
                        getDetails();//刷新详情
                        break;
                    case 3:
                        MToast.showToast(SalesOrderDetailsActivity.this, "接单成功");
                        getDetails();//刷新详情
                        finish();
                        break;
                }
            }
        });
        saleListViewModel.delete_fail.observe(this, id -> {
            EventBus.getDefault().post(new MakOverOrderDeleteItemEvent(id));
//            getDetails();
            finish();
        });
        saleListViewModel.closeFail.observe(this, id -> {
            EventBus.getDefault().post(new MakOverOrderDeleteItemEvent(id));
            getDetails();
        });
        saleListViewModel.conFrimOrderFail.observe(this, id -> {
            getDetails();
        });


        saleListViewModel.mToast.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                MToast.showToast(SalesOrderDetailsActivity.this, s);
            }
        });

    }

    private void setNetData(OderDetailsEntity data) {
        try {
            //联系人信息
            mBinding.tvContacts.setText(data.getContactName());//联系人
            mBinding.tvContactNumber.setText(data.getContactPhone());//联系电话
            mBinding.tvAddress.setText(data.getProvinceName() + data.getCityName() + data.getAreaName() + data.getAddress());//联系地址
            mBinding.tvZipCode.setText(data.getPostCode());//邮政编码
            mBinding.tvCompanyName.setText(data.getCompanyName());//公司名称
            mBinding.tvPosition.setText(data.getPosition());//职位
            //订单信息
            mBinding.tvOderNo.setText(data.getOrderId());//订单编号
            mBinding.tvBuyerAndSeller.setText(data.getBuyersName());//买卖方
            mBinding.tvPayType.setText("在线支付");//支付方式
            mBinding.tvHandler.setText(data.getBuyerAgent());//经办人
            mBinding.tvOderTime.setText(DateUtils.stampToDate(data.getOrderTime()));//下单时间
            mBinding.tvAllPrice.setText(TextViewBindingAdapter.setMoneyForRMB("￥" + PointUtils.price(Double.valueOf(data.getDealPrice()))));
            mBinding.tvMark.setEditContent(data.getComment());//备注
            mBinding.tvMark.setShowBottomNumber(false);
            mBinding.tvMark.setHint("/");
            mBinding.llMark.setVisibility(View.VISIBLE);
            mBinding.tvCheckInvoice.setVisibility(View.VISIBLE);
            mBinding.tvCheckPerson.setVisibility(View.VISIBLE);
            //订单状态码 1待接单 2待支付 3已关闭 4交易中 5交易完成

            switch (data.getOrderStatus()) {
                case "1":
                    mBinding.tvStatus.setText("待接单");
                    mBinding.tvCloseOder.setVisibility(View.VISIBLE); //关闭
                    mBinding.tvOrderReceiving.setVisibility(View.VISIBLE);//确认接单
                    mBinding.tvCheckPerson.setVisibility(View.GONE);//选择联系人
                    mBinding.tvCheckInvoice.setVisibility(View.GONE);//选择发票
                    mBinding.llMark.setVisibility(View.GONE);//编辑备注
                    saleListViewModel.orderStatus = 1;
                    //销售订单
                    mViewModel.timeBefore(data.getExpirationTime(), "后自动关闭订单", data.getSystemTime());
                    setDrawable(true);
                    saleListViewModel.buyerOrSeller = 0;
                    saleListViewModel.getUserWallet();
                    break;
                case "2":
                    mBinding.tvStatus.setText("待付款");
                    saleListViewModel.orderStatus = 2;
                    mBinding.tvEndTime.setText("等待买方付款");
                    setDrawable(false);
                    mBinding.llMark.setVisibility(View.GONE);//编辑备注
                    mBinding.tvCheckPerson.setVisibility(View.GONE);//选择联系人
                    mBinding.tvCheckInvoice.setVisibility(View.GONE);//选择发票
                    break;
                case "3":
                    mBinding.tvStatus.setText("已关闭");
                    saleListViewModel.orderStatus = 3;
                    mBinding.tvCloseOder.setVisibility(View.GONE); //关闭
                    mBinding.tvDeleteOder.setVisibility(View.VISIBLE);//删除
                    mBinding.tvOrderReceiving.setVisibility(View.GONE);//确认接单
                    mBinding.llMark.setVisibility(View.GONE);//编辑备注
                    mBinding.tvCheckPerson.setVisibility(View.GONE);//切换联系人
                    mBinding.tvCheckInvoice.setVisibility(View.GONE);//选择开票信息
                    break;
                case "4":
                    mBinding.llMark.setVisibility(View.GONE);
                    mBinding.tvCheckInvoice.setVisibility(View.VISIBLE);
                    mBinding.tvCheckPerson.setVisibility(View.GONE);
                    mBinding.tvStatus.setText("交易中");
                    mBinding.tvInvoiceMsg.setText("开票记录");
                    mBinding.itvVoucher.setVisibility(View.VISIBLE);
                    //是否显示提交凭证
                    if (data.getProcessStatus().equals("4")) {
                        mBinding.tvCommitVoucher.setVisibility(View.GONE);
                        mBinding.tvEndTime.setText("等待买方确认");
                    } else {
                        mBinding.tvCommitVoucher.setVisibility(View.VISIBLE);
                        mBinding.tvEndTime.setText("请及时处理并提交凭证");
                    }
                    break;
                case "5":
                    mBinding.llMark.setVisibility(View.GONE);
                    mBinding.tvCheckInvoice.setVisibility(View.VISIBLE);
                    mBinding.tvCheckPerson.setVisibility(View.GONE);
                    mBinding.tvStatus.setText("已完成");
                    mBinding.tvInvoiceMsg.setText("开票记录");
                    mBinding.itvVoucher.setVisibility(View.VISIBLE);
                    mBinding.tvCommitVoucher.setVisibility(View.GONE);
                    mBinding.tvEndTime.setVisibility(View.GONE);
                    RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mBinding.tvStatus.getLayoutParams();
                    lp.topMargin = DensityUtils.dip2px(this, 16);
                    mBinding.tvStatus.setLayoutParams(lp);
                    break;
            }
            //发票信息
            mBinding.tvInvoiceTitle.setText(data.getInvoiceTitle());//开票抬头
            mBinding.tvInvoiceNumber.setText(data.getNumber());//税号
            mBinding.tvBankOfDeposit.setText(data.getBank());//开户银行
            mBinding.tvBankNo.setText(data.getBankAccount());//银行账号
            mBinding.tvCompanyAddress.setText(data.getCompanyAddress());//企业地址
            mBinding.tvCompanyPhone.setText(data.getTel());//企业电话
            //知产信息
            String img = "";//知产图片
            int imgId = R.mipmap.image_order_list_defuntimage;
            //知产图片
            if (zhiChanType == TRADE_MARK) {
                img = data.getTrademarkImg();//商标图片
                mBinding.name.setText("商标名称：" + data.getTradeMarkName());//商标名称
                mBinding.tvType1.setText("商标分类：" + data.getTrademarkClassification());//商标分类
                mBinding.tvType2.setText("商标注册号：" + data.getTradeMarkRegistration());//商标注册号
                type = "商标";
            } else if (zhiChanType == PATENT) {
                //专利
                img = data.getPatentImg();
                mBinding.name.setText("专利名称：" + data.getPatentName());//专利名称
                mBinding.tvType1.setText("专利类型：" + data.getPatentClassification());//专利类型
                mBinding.tvType2.setText("专利注册号：" + data.getPatentRegistration());//专利注册号
                type = "专利";
                switch (data.getPatentClassification()) {
                    case "发明专利":
                        imgId = R.mipmap.qiyefuwu_module_famingzhuanli_for_patent_publish;
                        break;
                    case "实用新型":
                        imgId = R.mipmap.qiyefuwu_module_shiyongxinxing_patent_publish;
                        break;
                    case "外观设计":
                        imgId = R.mipmap.qiyefuwu_module_waiguansheji_patent_publish;
                        break;
                }
            } else if (zhiChanType == COPYRIGHT) {
                //版权
                img = data.getCopyrightImg();
                mBinding.name.setText("版权名称：" + data.getCopyrightName());//版权名称
                mBinding.tvType1.setText("版权分类：" + data.getClassificationCopyright());//版权分类
                mBinding.tvType2.setText("登记号：" + data.getCopyrightNumber());//登记号
                type = "版权";
            }
            //知产图片
            Glide.with(SalesOrderDetailsActivity.this)
                    .load(img)
                    .error(imgId)
                    .apply(new RequestOptions().transforms(new CenterCrop(), new RoundedCorners(2)))
                    .into(mBinding.img);//图标
            if (data.getOrderClassification().equals("2")) {
                mBinding.tvBargainingPrice.setVisibility(View.VISIBLE);
                mBinding.tvBargainingPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG); // 设置中划线并加清晰
                mBinding.tvBargainingPrice.setText("￥" + PointUtils.price(Double.valueOf(data.getTransferPrice())));
                mBinding.tvPrice.setText("￥" + PointUtils.price(Double.valueOf(data.getDealPrice())));
                mBinding.imageYj.setVisibility(View.VISIBLE);
            } else {
                mBinding.tvBargainingPrice.setVisibility(View.GONE);
                mBinding.tvPrice.setText(TextViewBindingAdapter.setMoneyForRMB("￥" + PointUtils.price(Double.valueOf(data.getTransferPrice()))));//转让价格
                mBinding.imageYj.setVisibility(View.GONE);
            }
            mBinding.tvCount.setText("x" + data.getAmount());//数量
        } catch (Exception e) {
            Log.e("销售订单详情", "数据异常");
        }
    }

    /**
     * 详情接口
     */
    private void getDetails() {
        mViewModel.removeTimer();
        //销售订单传2  采购订单传1
        if (zhiChanType == TRADE_MARK) {
            mViewModel.tradeMark(oderId, 2);
        } else if (zhiChanType == PATENT) {
            mViewModel.patent(oderId, 2);
        } else if (zhiChanType == COPYRIGHT) {
            mViewModel.copyrigh(oderId, 2);
        }
    }


    /**
     * 设置展开隐藏
     */
    private void setFold(ImageView imageView, TextView textView, LinearLayout linearLayout) {
        int gone = linearLayout.getVisibility();
        imageView.setImageResource(gone == View.GONE ? R.mipmap.icn_shouqi : R.mipmap.icn_zhankai);
        textView.setText(gone == View.GONE ? "收起" : "展开");
        linearLayout.setVisibility(gone == View.GONE ? View.VISIBLE : View.GONE);
    }

    /**
     * 查看流程节点
     */
    private void showOderNodeDialog(List<ServiceNodeBean> serviceNodeBeans) {
        if (oderNodeDialog == null) {
            oderNodeDialog = new OderNodeDialog(this, serviceNodeBeans);
        }
        oderNodeDialog.show();
    }

    /**
     * 切换联系人
     *
     * @param data
     */
    private void checkContacts(ContactsEntitiy data) {
        mViewModel.contactData.setAddress(data.getAddress());
        mViewModel.contactData.setAreaCode(data.getAreaCode());
        mViewModel.contactData.setAreaName(data.getAreaName());
        mViewModel.contactData.setCityCode(data.getCityCode());
        mViewModel.contactData.setCityName(data.getCityName());
        mViewModel.contactData.setCompanyName(data.getCompanyName());
        mViewModel.contactData.setContactId(data.getId() + "");
        mViewModel.contactData.setContactName(data.getContactName());
        mViewModel.contactData.setContactPhone(data.getContactPhone());
        mViewModel.contactData.setOrderId(mViewModel.oderData.getValue().getId());
        mViewModel.contactData.setPosition(data.getPosition());
        mViewModel.contactData.setPostCode(data.getPostCode());
        mViewModel.contactData.setProvinceCode(data.getProvinceCode());
        mViewModel.contactData.setProvinceName(data.getProvinceName());
        mViewModel.saveOrUpdate();
    }

    /**
     * 切换联系人
     *
     * @param data
     */
    private void checkInvoice(InvoicingInfoEntity data) {
        mViewModel.invoiceData.setBank(data.getBank());
        mViewModel.invoiceData.setBankAccount(data.getBankAccount());
        mViewModel.invoiceData.setInvoiceId(data.getId() + "");
        mViewModel.invoiceData.setInvoiceTitle(data.getCompanyName());
        mViewModel.invoiceData.setNumber(data.getNumber());
        mViewModel.invoiceData.setOrderId(mViewModel.oderData.getValue().getId());
        mViewModel.invoiceData.setTel(data.getTel());
        mViewModel.billingInformation();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case CONTACTS:
                if (RequestCode.CODE_ZERO == resultCode && data.getSerializableExtra("data") != null) {
                    checkContacts((ContactsEntitiy) data.getSerializableExtra("data"));
                }
                break;
            case INVOICE:
                if (RequestCode.CODE_ZERO == resultCode && data.getSerializableExtra("data") != null) {
                    checkInvoice((InvoicingInfoEntity) data.getSerializableExtra("data"));
                }
                break;
            case MARK:
                if (RequestCode.CODE_ZERO == resultCode && data.getSerializableExtra("data") != null) {
                    mBinding.tvMark.setEditContent((String) data.getSerializableExtra("data"));
                }
                break;
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_order_receiving) {
            //确认接单
            orderReceiving();
            operationOrder = 3;
        } else if (id == R.id.tv_close_oder) {
            //关闭订单
            oderClose();
        } else if (id == R.id.tv_delete_oder) {
            //删除订单
            oderDelete();
        } else if (id == R.id.ll_fold_person_info) {
            //展开收起
            setFold(mBinding.imgFoldPersonInfo, mBinding.tvFoldPersonInfo, mBinding.llGonePerson);
        } else if (id == R.id.ll_fold_oder_info) {
            //展开收起
            setFold(mBinding.imgFoldOderInfo, mBinding.tvFoldOderInfo, mBinding.llGoneOder);
        } else if (id == R.id.ll_fold_invoice_info) {
            //展开收起
            setFold(mBinding.imgFoldInvoiceInfo, mBinding.tvFoldInvoiceInfo, mBinding.llGoneInvoice);
        } else if (id == R.id.tv_check_person) {
            //切换联系人
            ActivityJump.startActivityForResult(SalesOrderDetailsActivity.this, CONTACTS, PathUser.ContanctsActivity, Pair.create("request", true));
        } else if (id == R.id.tv_check_invoice) {
            //选择发票信息
            if ("4".equals(mViewModel.oderData.getValue().getOrderStatus()) || "5".equals(mViewModel.oderData.getValue().getOrderStatus())) {
                //查看开票记录
                ActivityJump.startActivity(PathEnterpriseServices.BillingRecordActivity, Pair.create("oderId", oderId),
                        Pair.create("isGone", false), Pair.create("status", mViewModel.oderData.getValue().getProcessStatus()));
            } else {
                ActivityJump.startActivityForResult(SalesOrderDetailsActivity.this, INVOICE, PathUser.ReceIptActivity);
            }
        } else if (id == R.id.ll_mark) {
            //备注跳转
            ActivityJump.startActivityForResult(SalesOrderDetailsActivity.this, MARK, PathEnterpriseServices.SetMarkActivity, Pair.create("mark", mBinding.tvMark.getEditContent()), Pair.create("id", mViewModel.oderData.getValue().getId()));
        } else if (id == R.id.look_progress) {
            //查看进度
            showOderNodeDialog(serviceNodeBean);
        } else if (id == R.id.back_view) {
            finish();
        } else if (id == R.id.rv_go_to) {
            //详情跳转
            if (!ClickUtils.isDoubleClick()) {
                ActivityJump.startActivity(AppRouteConstant.ZiChan.ZICHAN_MAKEOVERDETAILACTIVITY, Pair.create("id", Long.valueOf(mViewModel.oderData.getValue().getDetailsId())), Pair.create("detail", type));
            }
        } else if (id == R.id.image_talk_im) {
            MToast.showLongToast(this, "功能开发中，敬请期待!");
        } else if (id == R.id.tv_commit_voucher) {
            //查看凭证记录
            ActivityJump.startActivity(PathEnterpriseServices.VoucherActivity, Pair.create("oderId", oderId),
                    Pair.create("isGone", false), Pair.create("status", mViewModel.oderData.getValue().getProcessStatus()), Pair.create("isList", false));
        }
    }

    @Override
    public void OnWholeItemClick(View view) {
        int id = view.getId();
        if (id == R.id.itv_database) {
            ActivityJump.startActivity(PathEnterpriseServices.DatabaseActivity, Pair.create("id", mViewModel.oderData.getValue().getId()),
                    Pair.create("buyerOrSeller", 1),
                    Pair.create("orderstatus", mViewModel.oderData.getValue().getOrderStatus()));
        } else if (id == R.id.itv_journal) {
//            MToast.showToast(this, "请前往PC端登录查看");
        } else if (id == R.id.itv_voucher) {
            //查看凭证记录
            ActivityJump.startActivity(PathEnterpriseServices.VoucherActivity, Pair.create("oderId", oderId),
                    Pair.create("isGone", false), Pair.create("status", mViewModel.oderData.getValue().getProcessStatus()), Pair.create("isList", false));
        }
    }

    /**
     * 确认接单
     */
    private void orderReceiving() {
        if (saleListViewModel.process_type == -1) {
            mViewModel.mToast.postValue("获取开户信息失败");
            return;
        }
        if (saleListViewModel.process_type == 2 || saleListViewModel.process_type == 4) {//已开户
            DialogHelper.showDialog(SalesOrderDetailsActivity.this,
                    "重要提示",
                    "确认接收该订单，若接单后，出现因卖方原因而造成异常订单，一律由卖方承担违约金（包括且不限于卖方损失费用、三方支付平台手续费等）。",
                    "不了，我再看看",
                    "明白仍要接单",
                    new DialogFunction() {
                        @Override
                        public void normalRoundDialog() {
                            saleListViewModel.ConFrimOrder(mViewModel.oderData.getValue().getId() + "");
                        }
                    });
        } else {
            int muser_type = SharedPreferenceUtil.getInstance().getSaveIntData(SharedPreferenceUtil.COM_TYPE, 0);
            switch (muser_type) {
                case 0://个人
                    DialogHelper.showDialog(SalesOrderDetailsActivity.this,
                            "无法接单",
                            "请先开通商家资金账户",
                            "商家资金账户开通后，销售订单可使用在线收款，支持网银收款、快捷支付收款等。",
                            "前往“资金中心”处理 ",
                            "取消",
                            "立即前往",
                            new DialogFunction() {
                                @Override
                                public void normalRoundDialog() {
                                    ActivityJump.startActivity(PathCapital.OpenAccountFirstActivity,
                                            Pair.create("isUser", muser_type == 0));
                                }
                            }
                    );
                    break;
                case 1://公司
                    DialogHelper.showDialog(SalesOrderDetailsActivity.this,
                            "无法接单",
                            "请先开通商家资金账户",
                            "商家资金账户开通后，销售订单可使用在线收款，支持企业网银收款、快捷支付收款等。",
                            "前往“资金中心”处理 ",
                            "取消",
                            "立即前往",
                            new DialogFunction() {
                                @Override
                                public void normalRoundDialog() {

                                    ActivityJump.startActivity(PathCapital.OpenAccountFirstActivity,
                                            Pair.create("isUser", muser_type == 0));
                                }
                            }
                    );
                    break;
                case 2://团队
                    DialogHelper.showDialog(SalesOrderDetailsActivity.this,
                            "无法接单",
                            "请先开通商家资金账户",
                            "商家资金账户开通后，销售订单可使用在线收款，支持网银收款、快捷支付收款等。",
                            "请切换为个人身份，前往【资金中心】进行开户。",
                            "",
                            "确认",
                            new DialogFunction() {
                                @Override
                                public void normalRoundDialog() {
                                    ActivityJump.startActivity(PathCapital.OpenAccountFirstActivity,
                                            Pair.create("isUser", muser_type == 0));
                                }
                            }
                    );
                    break;
            }
        }
    }

    /**
     * 关闭订单
     */
    private void oderClose() {
        DialogHelper.showDialog(SalesOrderDetailsActivity.this, "关闭订单", true, "请输入关闭订单的原因", 200, true, new DialogTextReturnValue() {
            @Override
            public void returnvalue(String value, Dialog dialog) {
                dialog.dismiss();
                operationOrder = 2;
                saleListViewModel.ColoseOrder(mViewModel.oderData.getValue().getId() + "", value, true);
            }
        });
    }


    /**
     * 关闭订单
     */
    private void oderDelete() {
        DialogHelper.showDialog(SalesOrderDetailsActivity.this, "温馨提示", "是否删除该订单？", "取消", "确定", new DialogFunction() {
            @Override
            public void normalRoundDialog() {
                operationOrder = 0;
                saleListViewModel.deleteOrder(mViewModel.oderData.getValue().getId());
            }
        });
    }

    /**
     * 显示隐藏 时间icn
     */
    private void setDrawable(boolean time) {
        if (time) {
            Drawable drawable = getResources().getDrawable(R.mipmap.icn_biao);
            drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
            mBinding.tvEndTime.setCompoundDrawables(drawable, null, null, null);
        } else {
            mBinding.tvEndTime.setCompoundDrawables(null, null, null, null);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateEvent(VoucherCommitEvent event) {
        if (event == null) {
            return;
        } else {
            if (event.status == 1)
                getDetails();
        }
    }
}
